﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;

namespace ModelValidation.ClearHeightCheck
{
	public class FillPatternManage
	{
		public static List<FillPatternStruc> GetFillPatternData(Autodesk.Revit.DB.Document doc, FillFatternType type)
		{
			string path = AssistFunc.GetAppRootPath() + "\\Data\\FillPatternConfig.xml";
			List<FillPatternStruc> list = new List<FillPatternStruc>();
			string text = Path.GetTempPath() + "FillPatternConfig.xml";
			if (!File.Exists(text))
			{
				File.AppendAllText(text, File.ReadAllText(path));
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(text);
			XmlNodeList xmlNodeList;
			if (type == FillFatternType.Color)
			{
				xmlNodeList = xmlDocument.SelectNodes("FillPatterns/ColorDefaultValue/FillPattern");
			}
			else
			{
				xmlNodeList = xmlDocument.SelectNodes("FillPatterns/DefaultValue/FillPattern");
			}
			for (int i = 0; i < xmlNodeList.Count; i++)
			{
				FillPatternStruc fillPatternStruc = new FillPatternStruc();
				if (i == 0)
				{
					fillPatternStruc.Start = -1.0;
					fillPatternStruc.Lessthan = Convert.ToDouble(xmlNodeList[i].Attributes[1].Value);
				}
				else if (i == xmlNodeList.Count - 1)
				{
					fillPatternStruc.Start = Convert.ToDouble(xmlNodeList[i].Attributes[0].Value);
					fillPatternStruc.Lessthan = -1.0;
				}
				else
				{
					fillPatternStruc.Start = Convert.ToDouble(xmlNodeList[i].Attributes[0].Value);
					fillPatternStruc.Lessthan = Convert.ToDouble(xmlNodeList[i].Attributes[1].Value);
				}
				fillPatternStruc.Title = xmlNodeList[i].Attributes[2].Value;
				fillPatternStruc.Visible = Convert.ToBoolean(xmlNodeList[i].Attributes[3].Value);
				string[] array = xmlNodeList[i].Attributes[4].Value.Split(new char[]
				{
					' '
				});
				fillPatternStruc.Color = System.Drawing.Color.FromArgb(Convert.ToInt32(array[0]), Convert.ToInt32(array[1]), Convert.ToInt32(array[2]));
				if (type == FillFatternType.Color)
				{
					fillPatternStruc.Id = FillPatternManage.GetSolidFillPatternId(doc);
					fillPatternStruc.Fillstyle = doc.GetElement(fillPatternStruc.Id).Name;
				}
				else
				{
					ElementId id = new ElementId(Convert.ToInt32(xmlNodeList[i].Attributes[5].Value));
					fillPatternStruc.Fillstyle = FillPatternManage.GetPatternById(doc, id).Name;
					fillPatternStruc.Id = id;
				}
				list.Add(fillPatternStruc);
			}
			return list;
		}

		private static ICollection<Element> GetFillPatternElementCollection(Autodesk.Revit.DB.Document doc)
		{
			return new FilteredElementCollector(doc).OfClass(typeof(FillPatternElement)).ToElements();
		}

		private static ElementId GetSolidFillPatternId(Autodesk.Revit.DB.Document doc)
		{
			ElementId result = null;
			foreach (Element element in FillPatternManage.GetFillPatternElementCollection(doc))
			{
				FillPatternElement fillPatternElement = element as FillPatternElement;
				if (fillPatternElement.GetFillPattern().IsSolidFill)
				{
					result = fillPatternElement.Id;
					break;
				}
			}
			return result;
		}

		private static FillPattern GetPatternById(Autodesk.Revit.DB.Document doc, ElementId id)
		{
			ICollection<Element> fillPatternElementCollection = FillPatternManage.GetFillPatternElementCollection(doc);
			FillPatternElement fillPatternElement = null;
			foreach (Element element in fillPatternElementCollection)
			{
				FillPatternElement fillPatternElement2 = element as FillPatternElement;
				if (fillPatternElement2.Id.IntegerValue == id.IntegerValue)
				{
					fillPatternElement = fillPatternElement2;
					break;
				}
			}
			if (fillPatternElement == null)
			{
				fillPatternElement = (fillPatternElementCollection.First<Element>() as FillPatternElement);
			}
			return fillPatternElement.GetFillPattern();
		}
	}
}
